home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-05-03 | 4.7 KB | 160 lines | [TEXT/CWIE] |
- /*
-
- ImportLDAPMeta
- Created 4/27/99
- Copyright 1999 Extensibility. All rights reserved.
-
- */
-
- import com.extensibility.convert.MetaSpectIntf;
- import java.util.Vector;
- import java.util.Hashtable;
- import java.util.Enumeration;
- import netscape.ldap.*;
-
- public class ImportLDAPMetaSample implements MetaSpectIntf {
-
- public static final String OID = "OID"; // value String
- String sourceString = "LDAPV3"; // The name of the importer itself, shows up in the XA menu
- String promptString = "Enter the hostname, userid and password for the LDAP source.";
- String listPrompt = "Select objectclasses to import.";
- Integer ldapport = new Integer(389);
- LDAPSchema dirSchema = new LDAPSchema();
-
- LDAPConnection lc;
-
- /** The way we keep track of objectclasses (tables) */
- class TableSpec implements MetaSpectIntf.TableIntf {
- String objectclass;
-
- public TableSpec(String objectclass){
- this.objectclass = objectclass;
- }
-
- /** Just the table name. */
- public String getShortName(){
- return objectclass;
- }
-
- public String getLongName(){
- return objectclass;
- }
-
- public String toString(){
- return getLongName();
- }
- }
-
- /* @see MetaSpectIntf.getPrompt(String) */
- public String getPrompt(int which) {
- switch (which){
- case PROMPT_IMPORTER:
- return sourceString;
- case PROMPT_SOURCE:
- return promptString;
- case PROMPT_LIST:
- return listPrompt;
- case PROMPT_REFINE:
- return null;
- }
- return "";
- }
-
- /**
- Opens a connection to an LDAP server, queries the schema and then
- closes the connection. Requires a connectstring that can either
- be equivalent to the hostname or the hostname and port:
- ldap.extensibility.com or ldap.extensibility.com:389
- The user string MUST be a distinguished name (DN) such as:
- cn=Directory Manager or uid=smith,o=Extensibility,c=US
- */
- public void open(String connectString, String user, String password) throws LDAPException{
- if (connectString.indexOf(":") >= 0) {
- ldapport = new Integer(connectString.substring(connectString.indexOf(":")+1));
- connectString = connectString.substring(0,connectString.indexOf(":"));
- }
- // Connect to LDAP server and acquire schema.
- LDAPConnection lc = new LDAPConnection();
- lc.connect(connectString,ldapport.intValue());
- if ((user != null)&&(password != null))
- lc.authenticate(user,password);
- dirSchema.fetchSchema( lc );
- }
-
- /**
- Return a list of objectclasses available.
- @see MetaSpectIntf.getChoiceList()
- */
- public Enumeration getChoiceList() {
- Vector myChoice = new Vector();
- Enumeration oc = dirSchema.getObjectClassNames();
- while (oc.hasMoreElements())
- myChoice.addElement(new TableSpec((String)oc.nextElement()));
- return myChoice.elements();
- }
-
- /**
- Return a particular objectclass.
- @see MetaSpectIntf.getMetaInfo(MetaSpectIntf.TableIntf)
- */
- public Enumeration getMetaInfo(MetaSpectIntf.TableIntf choice) {
- TableSpec tableInfo = (TableSpec) choice;
- LDAPObjectClassSchema myObject = dirSchema.getObjectClass(tableInfo.getShortName());
- Vector Attributes = new Vector();
- Enumeration ra = myObject.getRequiredAttributes();
- while (ra.hasMoreElements())
- Attributes.addElement( this.getOneColumn((String)ra.nextElement(), true));
- Enumeration oa = myObject.getOptionalAttributes();
- while (oa.hasMoreElements())
- Attributes.addElement( this.getOneColumn((String)oa.nextElement(), false));
- return Attributes.elements();
- }
-
- /**
- Return a particular attribute.
- */
- protected Hashtable getOneColumn(String columnName, boolean isRequired){
- Hashtable myColumn = new Hashtable(){
- // we override to catch null values
- public Object put(Object key, Object value){
- if (value == null)
- return null;
- return super.put(key, value);
- }
- };
- LDAPAttributeSchema attribute = dirSchema.getAttribute(columnName);
- myColumn.put(NAME, attribute.getName() );
- myColumn.put(DATATYPE, new Integer(attribute.getSyntax()));
- myColumn.put(NATIVEDATATYPE, attribute.getValue() );
- myColumn.put(REMARKS, attribute.getDescription() );
- if (!attribute.isSingleValued()) {
- myColumn.put(IS_ARRAY, Boolean.TRUE);
- }
- myColumn.put(OID, attribute.getOID() );
- myColumn.put(ISNULLABLE, new Boolean(!isRequired));
- myColumn.put(IS_ID, Boolean.FALSE);
- return myColumn;
- }
-
-
- // Stub for MetaSpectIntf inteface.
- public java.util.Enumeration getRefineList(MetaSpectIntf.TableIntf myTable) {
- return null;
- }
-
- // Stub for MetaSpectIntf implementation. No function since the
- // connection is closed immediately after retrieving schema information.
- // @open, above.
- public void close() {
- if (lc == null)
- return;
- try {
- lc.disconnect();
- }
- catch (LDAPException f) {
- // do nothing
- }
-
- }
-
- }